const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
/**
 * 改变验证码按钮状态
 */
function verifyCodeBtn(e, that) {
  if (e.currentTarget.id == 'user' && (/^1[3456789]\d{9}$/.test(e.detail.value))) {
    that.setData({
      vcdisabled: false,
      btn: true
    })
  } else if (e.currentTarget.id == 'user' && !(/^1[3456789]\d{9}$/.test(e.detail.value))) {
    that.setData({
      vcdisabled: true
    })
  }
}


//上传图片
async function uploadFileTest(filePath, credentials, suffix) {
  // console.log('---------上传图片')
  return new Promise((resolve) => {
    wx.uploadFile({
      url: credentials.host, //仅为示例，非真实的接口地址
      filePath: filePath,
      name: 'file',
      formData: {
        "policy": credentials.policy,
        "key": credentials.dir + credentials.fileName + '.' + suffix,
        "OSSAccessKeyId": credentials.accessid,
        "Signature": credentials.signature,
        "success_action_status": 200,
        "fileName": credentials.fileName,
      },
      success: () => {
        // console.log('---------上传图片成功')
        resolve(credentials.fileName + '.' + suffix)
      },
      fail: (err) => {
        console.log('---------上传失败', err)
      }
    })
  })

}
module.exports = {
  formatTime,
  verifyCodeBtn: verifyCodeBtn,
  uploadFileTest
}